from django.http import JsonResponse
from web.models import Collection, CollectionItem
from django import forms
from django.forms.models import model_to_dict
from datetime import datetime


def return_json_response(status, data):
    return JsonResponse({'status': status, 'data': data})

# 返回时间
def return_time(dict_data, model_data):
    dict_data['created_at'] = model_data.created_at.strftime('%Y-%m-%d %H:%M:%S')
    return dict_data

# 收藏夹访问次数+1
def collection_visit_count_add(tkid):
    collection = Collection.objects.filter(tkid=tkid).first()
    if collection is not None:
        collection.visit_count += 1
        collection.save()
        return True
    else:
        return False

# 删除收藏夹项目的某些属性
def delete_collection_item(collection_item_dict):
    del collection_item_dict['id']
    return collection_item_dict

class CollectionForm(forms.ModelForm):
    class Meta:
        model = Collection
        fields = ['name']

# 创建收藏夹
def create_collection(request):
    if request.method == 'POST':
        form = CollectionForm(request.POST)
        if form.is_valid():
            collection = form.save()
            new_collection = model_to_dict(collection)
            return_time(new_collection, collection)
            return return_json_response(0, new_collection)
        else:
            return return_json_response(1, form.errors)
    else:
        return return_json_response(1, '无效的请求方法')



# 创建收藏夹项目
class CollectionItemForm(forms.ModelForm):
    class Meta:
        model = CollectionItem
        fields = '__all__'

# 创建收藏夹项目
def create_collection_item(request):
    if request.method == 'POST':
        form = CollectionItemForm(request.POST)
        if form.is_valid():
            collection_item = form.save()
            new_collection_item = model_to_dict(collection_item)
            return_time(new_collection_item, collection_item)
            return return_json_response(0, new_collection_item)
        else:
            return return_json_response(1, form.errors)
    else:
        return return_json_response(1, '无效的请求方法')

# 获取收藏夹项目列表
def get_collection_item_list(request):
    try:
        # 获取收藏夹列表
        tkid = request.GET.get('tkid')
        if tkid is None:
            return return_json_response(1, 'tkid不能为空')
        collection_items = CollectionItem.objects.filter(tkid=tkid).order_by('-created_at')
        if collection_items is None:
            return return_json_response(1, '收藏夹不存在')
        new_collection_items = []   # 返回的收藏夹项目列表
        for collection_item in collection_items: # 遍历收藏夹项目
            new_collection_item = model_to_dict(collection_item) # 转换为字典
            new_collection_item = delete_collection_item(new_collection_item) # 删除属性
            new_collection_items.append(return_time(new_collection_item, collection_item)) # 添加到返回列表
        
        # 收藏夹访问次数+1
        if collection_visit_count_add(tkid) is not True:
            return return_json_response(1, '收藏夹不存在')
        
        return return_json_response(0, new_collection_items)
    except Exception as e:
        return return_json_response(1, str(e))

        
